package cn.wikiflyer.beikang.act.user;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.utils.WKUmShareUtils;

/**
 * 推荐好友
 * 
 * @author qunlee_mr
 * 
 */
public class UserShareAct extends BaseActivity {

	@ViewInject(id = R.id.usershare_message, click = "onClick")
	private Button message;
	@ViewInject(id = R.id.usershare_circle, click = "onClick")
	private Button circle;
	@ViewInject(id = R.id.usershare_weixin, click = "onClick")
	private Button weixin;
	@ViewInject(id = R.id.usershare_copy, click = "onClick")
	private Button copy;

	// 友盟社会化分享
	public final UMSocialService mController = UMServiceFactory
			.getUMSocialService("com.umeng.share");

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_share);
	}

	@Override
	public void init() {
	}

	@Override
	public void setData() {
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.usershare_message:
			sendSMS();
			break;
		case R.id.usershare_circle:
			// WKApplication.showToast("朋友圈");
			share(false);
			break;
		case R.id.usershare_weixin:
			// WKApplication.showToast("好友");
			share(true);
			break;
		case R.id.usershare_copy:
			copy(app().shareTuijian.share_text + app().getShareCode() + " "
					+ app().shareTuijian.share_url, this);
			WKApplication.showToast("已复制推荐链接");
			break;

		default:
			break;
		}
	}

	// String msg = "邀请您加入倍康，财富和健康双收，2015会员限时开放注册，加入我们开启您新的事业，"
	// + "点击链接注册成为倍康会员，尊享更多优惠和特权：t.cn/d34d54";
	// String title = "倍康";
	// String url = "http://a07186074.atobo.com.cn";

	String weixinAppID = "wxff07008c39db9493";
	String weixinAppSecret = "85bdf2461e3a489400204a44ed03146d";

	private void sendSMS() {
		Uri uri = Uri.parse("smsto:");
		Intent it = new Intent(Intent.ACTION_SENDTO, uri);
		it.putExtra("sms_body", "" + app().shareTuijian.share_text
				+ app().getShareCode() + " " + app().shareTuijian.share_url);
		activity.startActivity(it);
	}

	// 微信／微信朋友圈分享
	private void share(boolean weixin) {
		if (weixin) {
			// 添加微信平台
			UMWXHandler wxHandler = new UMWXHandler(ctx, weixinAppID,
					weixinAppSecret);
			wxHandler.addToSocialSDK();
			// 设置微信好友分享内容
			WeiXinShareContent weixinContent = new WeiXinShareContent();
			// 设置分享文字
			weixinContent
					.setShareContent(app().shareTuijian.share_text
							+ app().getShareCode() + " "
							+ app().shareTuijian.share_url);
			// 设置title
			weixinContent.setTitle(app().shareTitle);
			// weixinContent.setShareImage(app().shareImage);
			// 设置分享内容跳转URL
			weixinContent.setTargetUrl(app().shareTuijian.share_url);
			// 临时跳转到首页
			if (app().shareHomePageToWEIXIN) {
				weixinContent.setTargetUrl("http://www.qhbeikang.com");
			}
			mController.setShareMedia(weixinContent);
			mController.postShare(this, SHARE_MEDIA.WEIXIN, postListener);
		} else {
			// 支持微信朋友圈
			UMWXHandler wxCircleHandler = new UMWXHandler(ctx, weixinAppID,
					weixinAppSecret);
			wxCircleHandler.setToCircle(true);
			wxCircleHandler.addToSocialSDK();

			// 设置微信朋友圈分享内容
			CircleShareContent circleMedia = new CircleShareContent();
			circleMedia
					.setShareContent(app().shareTuijian.share_text
							+ app().getShareCode() + " "
							+ app().shareTuijian.share_url);
			// 设置朋友圈title
			circleMedia.setTitle(app().shareTitle);
			// circleMedia.setShareImage(app().shareImage);
			circleMedia.setTargetUrl(app().shareTuijian.share_url);
			// 历史跳转到首页
			if (app().shareHomePageToWEIXIN) {
				circleMedia.setTargetUrl("http://www.qhbeikang.com");
			}
			mController.setShareMedia(circleMedia);
			mController
					.postShare(this, SHARE_MEDIA.WEIXIN_CIRCLE, postListener);
		}
	}

	private SnsPostListener postListener = new SnsPostListener() {

		@Override
		public void onStart() {

		}

		@Override
		public void onComplete(SHARE_MEDIA platform, int eCode,
				SocializeEntity entity) {
			if (eCode == 200) {
				Toast.makeText(ctx, "分享成功.", Toast.LENGTH_SHORT).show();
			} else {
				String eMsg = "";
				if (eCode == -101) {
					eMsg = "没有授权";
				}
				Toast.makeText(ctx, "分享失败[" + eCode + "] " + eMsg,
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	/**
	 * 实现文本复制功能 add by wangqianzhou
	 * 
	 * @param content
	 */
	public static void copy(String content, Context context) {
		// 得到剪贴板管理器
		ClipboardManager cmb = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		cmb.setText(content.trim());
	}

}
